var app = getApp();
Component({
	options: {
		addGlobalClass: true,
	},
	data: {
		loginInformation: true,
		dealer: {},
		starCount: 0,
		cardCur: 0,
		userInfo: wx.getStorageSync('userInfo'),
		swiperList: [],
		categoryList: [],
		goodsList: [],
		search: '',
		pages: 0,
		page: 1
	},
	attached() {
		var that = this;
		this.setData({
			dealer: wx.getStorageSync('dealer'),
			// loginInformation: wx.getStorageSync('loginInformation'),
			userInfo: wx.getStorageSync('userInfo')
		});
		app.globalAjax(
			'/api/banner/list',
			"get", {},
			function(e) {
				if (e.code == 0) {
					that.setData({
						swiperList: e.bannerList
					})
				} else {
					wx.showToast({
						title: e.msg,
						icon: "none"
					});
				}
			})


		app.globalAjax(
			'/api/category/list',
			"get", {},
			function(e) {
				if (e.code == 0) {
					that.setData({
						categoryList: e.categoryList
					})

				} else {
					wx.showToast({
						title: e.msg,
						icon: "none"
					});
				}
			})


		app.globalAjax(
			'/api/goods/list',
			"get", {
				isRecommend: 1,
				page: 1,
				limit: 50
			},
			function(e) {
				if (e.code == 0) {
					that.setData({
						goodsList: e.goodsList.list,
						pages: e.goodsList.totalPage
					})
				} else {
					wx.showToast({
						title: e.msg,
						icon: "none"
					});
				}
			})

	},
	methods: {
		searchIcon(e) { //输入
			this.setData({
				search: e.detail.value
			})
		},
		fetchArticleList(pageNo) {
			var $th = this;
			var post = {
				isRecommend: 1,
				page: pageNo,
				limit: 10
			}
			app.globalAjax(
				'/api/goods/list',
				"get",
				post,
				function(e) {
					if (e.code == 0) {
						$th.setData({
							goodsList: $th.data.goodsList.concat(e.goodsList.list)
						})
					} else {
						wx.showToast({
							title: e.msg,
							icon: "none"
						});
					}
				})
		},
		onReachBottom() {
			// 下拉触底，先判断是否有请求正在进行中
			// 以及检查当前请求页数是不是小于数据总页数，如符合条件，则发送请求
			if (!this.loading && this.data.page < this.data.pages) {
				this.data.page += 1
				this.fetchArticleList(this.data.page)
			}
		},
		search() {
			if (this.data.search == "") {
				wx.showToast({
					title: "请输入商品名称",
					icon: "none"
				});
				return false;
			}
			wx.navigateTo({
				url: '/pages/classify/productlist/productlist?keys=' + this.data.search
			})
		}
	},
	DotStyle(e) {
		this.setData({
			DotStyle: e.detail.value
		})
	},
	// cardSwiper
	cardSwiper(e) {
		this.setData({
			cardCur: e.detail.current
		})
	},
	// towerSwiper
	// 初始化towerSwiper
	towerSwiper(name) {
		let list = this.data[name];
		for (let i = 0; i < list.length; i++) {
			list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
			list[i].mLeft = i - parseInt(list.length / 2)
		}
		this.setData({
			swiperList: list
		})
	},
	// towerSwiper触摸开始
	towerStart(e) {
		this.setData({
			towerStart: e.touches[0].pageX
		})
	},
	// towerSwiper计算方向
	towerMove(e) {
		this.setData({
			direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
		})
	},
	// towerSwiper计算滚动
	towerEnd(e) {
		let direction = this.data.direction;
		let list = this.data.swiperList;
		if (direction == 'right') {
			let mLeft = list[0].mLeft;
			let zIndex = list[0].zIndex;
			for (let i = 1; i < list.length; i++) {
				list[i - 1].mLeft = list[i].mLeft
				list[i - 1].zIndex = list[i].zIndex
			}
			list[list.length - 1].mLeft = mLeft;
			list[list.length - 1].zIndex = zIndex;
			this.setData({
				swiperList: list
			})
		} else {
			let mLeft = list[list.length - 1].mLeft;
			let zIndex = list[list.length - 1].zIndex;
			for (let i = list.length - 1; i > 0; i--) {
				list[i].mLeft = list[i - 1].mLeft
				list[i].zIndex = list[i - 1].zIndex
			}
			list[0].mLeft = mLeft;
			list[0].zIndex = zIndex;
			this.setData({
				swiperList: list
			})
		}
	}
})
